package com.qishenyuan.competition.vo;

import com.fasterxml.jackson.core.type.TypeReference;
import com.qishenyuan.competition.Bo.EducationExperience;
import com.qishenyuan.competition.Bo.InternshipExperience;
import com.qishenyuan.competition.Bo.ProjectExperience;
import com.qishenyuan.competition.model.FindJob;
import com.qishenyuan.competition.utils.AliOssUtil;
import com.qishenyuan.competition.utils.DateUtil;
import com.qishenyuan.competition.utils.JacksonUtil;
import lombok.Data;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author Jacky
 * @since 2025-03-26
 */
@Data
public class FindJobResp {
    private Integer id;

    private String userName;

    private String avatar;

    private String school;

    private String title;

    private String advantage;

    private String major;

    private String expectedJob;

    private Integer expectedSalary;

    private List<ProjectExperience> projectExperiences;

    private List<EducationExperience> educationExperiences;

    private List<InternshipExperience> internshipExperiences;

    private List<String> certificates;

    private String createTime;

    private String content;

    private Integer tag;

    private Integer type;

    public static FindJobResp buildVo(FindJob findJob){
        FindJobResp resp = new FindJobResp();
        resp.setId(findJob.getId());
        resp.setTitle(findJob.getTitle());
        resp.setAdvantage(findJob.getAdvantage());
        resp.setMajor(findJob.getMajor());
        resp.setExpectedJob(findJob.getExpectedJob());
        resp.setExpectedSalary(findJob.getExpectedSalary());
        resp.setProjectExperiences(JacksonUtil.toObject(findJob.getProjectExperience(),new TypeReference<List<ProjectExperience>>() {}));
        resp.setEducationExperiences(JacksonUtil.toObject(findJob.getEducationExperience(),new TypeReference<List<EducationExperience>>() {}));
        resp.setInternshipExperiences(JacksonUtil.toObject(findJob.getInternshipExperience(),new TypeReference<List<InternshipExperience>>(){}));
        List<String> imageUrls = Optional.ofNullable(findJob.getCertificates())
                .map(FindJobResp::string2List)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(AliOssUtil::generatePreSignedUrl)
                .collect(Collectors.toList());
        resp.setCertificates(imageUrls);
        resp.setCreateTime(DateUtil.format(findJob.getCreateTime(),DateUtil.FORMAT_WITH_SPLIT));
        resp.setContent(findJob.getContent());
        resp.setTag(findJob.getTag());
        resp.setType(findJob.getType());
        return resp;
    }

    // 把String转换成list，用“;”分隔开
    private static List<String> string2List(String str) {
        List<String> result = Arrays.asList(str.split(";"));
        result = result.stream().filter(s -> !s.isEmpty()).collect(Collectors.toList());
        return result;
    }
}
